home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: Mike Girou <girou@parashift.com>
- Newsgroups: comp.lang.c++,rb.technical
- Subject: Re: What is "placement new" - was Re: Can copy constructor and operator= share code?
- Date: Thu, 07 Mar 1996 11:26:16 -0600
- Organization: Paradigm Shift, Inc.
- Message-ID: <313F1C38.462A@parashift.com>
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred> <NICKC.96Mar7142042@uxe.liv.ac.uk>
- NNTP-Posting-Host: ix-dfw9-03.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-NETCOM-Date: Thu Mar 07 9:26:35 AM PST 1996
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Spider plant breeding program wrote:
- >
- > In article <VA.00000053.00cdab05@fred> Frederic LACHASSE <lachass@worldnet.fr> writes:
- >
- > > From: Frederic LACHASSE <lachass@worldnet.fr>
- > > Generally, the operator=() must release old resource and create a copy
- > > of the other object. So a generic operator=() can be:
- > >
- > > T &T::operator =(const T &t)
- > > {
- > > if (this != &t) // if objects are same, nothing to do
- > > {
- > > ~T(); // explicit call to destructor to release resources.
- >
- > Shouldn't this be this->~T();
- >
- > > new(this) T(t); // use of the placement operator to call the
- > > // copy constructor.
- > > }
- >
- > return *this;
- >
- > > }
- > >
- > > Some times though, the assignement operator can be optimize to reuse
- > > resources of the old object.
- >
- > What is ths placement new operator?
- >
- > It's mentioned in passing in the FAQ, but never defined.
- >
- > No compiler I can run recognises this syntax
- > (I've tried gcc 2.6.3, Solaris C++ (V4 I think), Irix C++)
- >
-
- Placement new is a way to do a "new" off of previously allocated
- memory, and is important for efficiency reasons, such as buffer
- management. See Stroustrup's fundamental book for a good description.
-
- Mike
-
- > The problem I'm trying to solve is:
- >
- > class a
- > {
- > int *i;
- > const double &d; // Actually something more complex
- >
- > public:
- > a( const double &d_ ) : i( new int[3] ), d( d_ ) {};
- > ~a() { delete i; };
- > };
- >
- > How do I write operator = ( const class &a ) ?
- >
- > Thanks in advance,
- >
- > Nicholas Clark
- > --
- > C:\> ECHO f 0000:0000 ffff 66 | DEBUG
-
- --
- Mike Girou girou@parashift.com
-